package com.guacun.dao;


import com.guacun.model.Student;

public class StudentDao {
    public static Student[] studentinfo = new Student[100];
                 static {
                     for (int i = 0; i < 10; i++) {
                         studentinfo[i]=new Student(i+1,"学生"+(i+1),11+i,"2000-"+2+"-"+(i+i));
                     }

                 }
    public int add(Student[] list) {
        int flag = 0;
        for (int i = 0; i < studentinfo.length; i++) {
            if (null == studentinfo[i]) {
                studentinfo[i] = list[0];
                flag = 1;
                System.out.println("添加学生成功！");
                break;
            } else {
                System.out.println("添加学生失败！");
            }
        }

        return flag;
    }

    public int selectall() {
        for (int i = 0; i < studentinfo.length; i++) {
            if (null != studentinfo[i]) {
                System.out.println(studentinfo[i]);
            }
        }
        int i = 1;
        return i;
    }

    public int deletestudent(int id) {
        int i = 0;
        for (int j = 0; j < studentinfo.length; j++) {
            if (studentinfo[j].getId() == id) {
                for (int k = j; k < studentinfo.length - 1; k++) {
                    studentinfo[k] = studentinfo[k + 1];
                }
                i = 1;
                break;
            }
        }
        return i;
    }

    public Student selectallbyid(int id) {
        Student student = null;

        for (int i = 0; i < studentinfo.length; i++) {
            try {
                if (studentinfo[i].getId() == id) {
                    student = studentinfo[i];
                }
            } catch (NullPointerException e) {
              //  System.out.println("捕捉到的异常：" + e.getMessage());
            }
        }


        return student;
    }

    public int updatestudentdao(Student student) {
        int i = 0;
        for (int k = 0; k < studentinfo.length; k++) {
            try {
                if (studentinfo[k].getId() == student.getId()) {
                    studentinfo[k] = student;
                    i = 1;
                }
            } catch (NullPointerException e) {
                //System.out.println("捕捉到的异常：" + e.getMessage());
            }
        }
        return i;
    }
}
